<template>
  <div class="app-container" v-loading="loading">
    <!--功能按钮-->
    <div class="action-container">
      <el-button v-if="addStoreFlag" type="primary" icon="el-icon-plus" size="small" @click="openAddDialog">添加门店
      </el-button>
      <el-button icon="el-icon-refresh" size="small" @click="refresh">刷新</el-button>
    </div>
    <!--表格数据-->
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="name"
        label="门店名称">
      </el-table-column>
      <el-table-column
        prop="address"
        label="门店地址">
      </el-table-column>
      <el-table-column
        prop="username"
        label="店长账号">
      </el-table-column>
      <el-table-column
        prop="foodType"
        label="餐饮行业">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间">
      </el-table-column>
      <el-table-column
        label="操作"
        align="left" max-width="30">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="openEditDialog(scope.row)">编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <pagination
      :total-elements="totalElements"
      :change-callback="getTableList"
      ref="page">
    </pagination>

    <!--添加门店-->
    <el-dialog title="添加门店" :visible.sync="addDialog">
      <el-form :model="form">
        <el-form-item label="餐饮行业" :label-width="formLabelWidth">
          <el-select
            v-model="form.foodTypeId"
            collapse-tags
            placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="门店地址" :label-width="formLabelWidth">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialog = false">取 消</el-button>
        <el-button type="primary" @click="addStore">确 定</el-button>
      </div>
    </el-dialog>

    <!--修改门店-->
    <el-dialog title="修改门店" :visible.sync="editDialog">
      <el-form :model="form">
        <el-form-item label="餐饮行业" :label-width="formLabelWidth">
          <el-select
            v-model="form.foodTypeId"
            collapse-tags
            placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="门店地址" :label-width="formLabelWidth">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialog = false">取 消</el-button>
        <el-button type="primary" @click="editStore">确 定</el-button>
      </div>
    </el-dialog>

  </div>


</template>

<script>
  import {getTableList, findUserType, addStore,editStore} from '@/modules/store/api/store'
  import pagination from '@/components/pagination/index'

  export default {
    name: 'merchant',
    data () {
      return {
        addStoreFlag: false, // 是否显示添加门店按钮
        addDialog: false,
        tableData: [], // 表格数据
        totalElements: 0, // 数据条数
        multipleSelection: [], // 选中数据-多选
        editDialog: false, // 编辑模态框
        loading: true, // loading效果
        formLabelWidth: '120px',//设置宽度
        btnLoading: false, // 按钮loading
        //创建模态框src
        createdSrc: '',
        //编辑模态框src
        editSrc: '',
        tablePhotoVisible: false,
        editName: '',
        form: {
          name: '',
          foodTypeId: '',
          address: '',
        },
        options: [
          {value: '江浙菜', name: '江浙菜'},
          {value: '粤菜川菜', name: '粤菜川菜'},
          {value: '湘菜', name: '湘菜'},
          {value: '东北菜', name: '东北菜'},
          {value: '徽菜', name: '徽菜'},
          {value: '闽菜', name: '闽菜'},
          {value: '鲁菜', name: '鲁菜'},
          {value: '台湾菜', name: '台湾菜'},
          {value: '西北菜', name: '西北菜'},
          {value: '东南亚菜', name: '东南亚菜'},
          {value: '西餐', name: '西餐'},
          {value: '火锅', name: '火锅'},
          {value: '清真菜', name: '清真菜'},
          {value: '海鲜', name: '海鲜'},
          {value: '烧烤', name: '烧烤'},
          {value: '自助餐', name: '自助餐'},
          {value: '面包甜点', name: '面包甜点'},
          {value: '茶餐厅', name: '茶餐厅'},
          {value: '其他美食', name: '其他美食'},
        ],
        headers: {
          accessToken: sessionStorage.token
        }
      }
    },
    components: {pagination},
    created () {
      this.getTableList()
      this.findUserType()
    },
    methods: {
      openAddDialog () {
        this.form.name = ''
        this.form.foodTypeId = ''
        this.form.address = ''
        this.addDialog = true
      },
      openEditDialog (row) {
        this.form.id = row.id
        this.form.name = row.name
        this.form.foodTypeId = row.foodTypeId
        this.form.address = row.address
        this.editDialog = true
      },
      //表格列表
      getTableList: function (number, pageSize) {
        this.loading = true
        getTableList(number, pageSize).then(response => {
          let data = response.obj
          this.tableData = data.content
          this.totalElements = data.totalElements
          console.log("total",this.totalElements)
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      },

      /**
       * 判断登录用户是商户还是店长
       */
      findUserType: function () {
        findUserType().then(response => {
          if (response.obj == 1) {
            this.addStoreFlag = true
          }
        }).catch(() => {
        })
      },

      /**
       * 添加门店
       */
      addStore () {
        this.loading = true
        this.btnLoading = true
        addStore(this.form).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.addDialog = false
          this.$refs.page.refresh()
        }).catch(() => {
          this.btnLoading = false
          this.loading = false
        })
      },

      /*
     * 修改门店
     * */
      editStore () {
        this.loading = true
        this.btnLoading = true
        editStore(this.form).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.editDialog = false
          this.$refs.page.refresh()
        }).catch(() => {
          this.btnLoading = false
          this.loading = false
        })
      },

      /*
       * 刷新
       * */
      refresh () {
        this.$refs.page.refresh()
      },
    }
  }
</script>

<style scoped>
  .logo-uploader {
    display: inline-block;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .logo-uploader:hover {
    border-color: #409EFF;
  }

  .logo-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .logo {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
